package com.dianzi.certification.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dianzi.certification.dto.SchoolInvestigateDTO;
import com.dianzi.certification.entity.Certification;
import com.dianzi.certification.entity.SchoolInvestigate;
import com.dianzi.certification.mapper.SchoolInvestigateMapper;
import com.dianzi.certification.service.CertificationService;
import com.dianzi.certification.service.SchoolInvestigateService;
import com.dianzi.certification.vo.CertificationVO;
import com.dianzi.certification.vo.R;
import com.dianzi.certification.vo.SchoolInvestigateVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

import static com.dianzi.certification.constants.CertConstant.*;
import static com.dianzi.certification.constants.CertStatusConstant.CERTING;
import static com.dianzi.certification.constants.CertStatusConstant.CERT_SUCCESS;

/**
 * @author copa
 * @createDate 2022-03-29 23:11
 * @function
 */
@Service
public class SchoolInvestigateServiceImpl implements SchoolInvestigateService {

    @Resource
    private SchoolInvestigateMapper schoolInvestigateMapper;

    @Resource
    private CertificationService certificationService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R<String> addInvest(SchoolInvestigateDTO schoolInvestigateDTO, Integer userId) {
        Certification cert = certificationService.getOneCertByCertStageAndPrincipalId(
                PROFESSION_SELF_EVALUATION, userId, null, CERT_SUCCESS);
        if (Objects.isNull(cert)) {
            return R.errorMsg("请求错误~没有该数据~");
        }
        SchoolInvestigate investigate = new SchoolInvestigate();
        BeanUtils.copyProperties(schoolInvestigateDTO, investigate);
        investigate.setCertId(cert.getId());
        int insert = schoolInvestigateMapper.insert(investigate);
        if (insert < 1) {
            return R.errorMsg("系统繁忙~请稍后再试~");
        }
        cert.setCertStatus(CERTING)
                .setCertStage(SCHOOL_INVESTIGATE);
        if (certificationService.put(cert) < 1) {
            return R.errorMsg("系统繁忙~请稍后再试~");
        }
        return R.successMsg("新增考察成功~等待审核~");
    }

    @Override
    public R<List<SchoolInvestigateVO>> getInvest(Integer userId) {
        List<SchoolInvestigateVO> professionSelfEvaluationVOList =
                schoolInvestigateMapper.selectInvestByUserId(userId);
        return R.success(professionSelfEvaluationVOList);
    }

    @Override
    public R<SchoolInvestigate> getInvestDetail(Integer investId, Integer userId) {
        SchoolInvestigate schoolInvestigate = schoolInvestigateMapper.selectById(investId);
        R<CertificationVO> certR = certificationService.getCert(SCHOOL_INVESTIGATE, userId);
        if (certR.isNotSuccess() || Objects.isNull(certR.getData())) {
            return R.errorMsg(certR.getMsg());
        }
        return R.success(schoolInvestigate);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R<SchoolInvestigate> putInvest(SchoolInvestigateDTO schoolInvestigateDTO, Integer userId) {
        Certification cert = certificationService.getOneCertByCertStageAndPrincipalId(
                SCHOOL_INVESTIGATE, userId, null, CERTING
        );
        if (Objects.isNull(cert)) {
            return R.errorMsg("该记录现阶段不能被修改~");
        }
        SchoolInvestigate schoolInvestigate = new SchoolInvestigate();
        BeanUtils.copyProperties(schoolInvestigateDTO, schoolInvestigate);
        schoolInvestigate.setCertId(cert.getId());
        int update = schoolInvestigateMapper.updateById(schoolInvestigate);
        if (update < 1) {
            return R.errorMsg("系统繁忙~请稍后再试~");
        }
        return R.successMsg("修改成功~等待审核~");
    }

    @Override
    public R<SchoolInvestigate> getInvestDetailAdmin(Integer certId) {
        Certification cert = certificationService.getOneCertByCertStageAndPrincipalId(
                SCHOOL_INVESTIGATE, null, certId, CERTING
        );
        if (Objects.isNull(cert)) {
            return R.errorMsg("没有查到该进校考察记录~");
        }
        LambdaQueryWrapper<SchoolInvestigate> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(true, SchoolInvestigate::getCertId, certId);
        SchoolInvestigate schoolInvestigate = schoolInvestigateMapper.selectOne(wrapper);
        if (Objects.isNull(schoolInvestigate)) {
            return R.errorMsg("没有查到该v记录~");
        }
        return R.success(schoolInvestigate);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R<String> putInvestAdmin(Integer certId, Integer status, String remark) {
        R<SchoolInvestigate> proR = getInvestDetailAdmin(certId);
        if (proR.isNotSuccess()) {
            return R.errorMsg(proR.getMsg());
        }
        R<String> res = certificationService.putCertAdmin(certId, status, remark, SCHOOL_INVESTIGATE);
        if (1 == status) {
            // 如果通过，需要填备注
            SchoolInvestigate investigate = proR.getData();
            investigate.setRemark(remark);
            int update = schoolInvestigateMapper.updateById(investigate);
            if (update < 1) {
                return R.errorMsg("系统繁忙~请稍后再试~");
            }
        }
        if (res.isNotSuccess()) {
            return R.errorMsg(res.getMsg());
        }
        return R.successMsg("操作成功~");
    }
}
